package com.oocl.o2o.admin.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import com.oocl.o2o.admin.dao.ComplaintInfoDao;
import com.oocl.o2o.admin.entity.Advertisement;
import com.oocl.o2o.admin.entity.ComplaintInfo;
import com.oocl.o2o.admin.entity.Merchant;

@Repository(value="complaintInfoDaoImpl")
public class ComplaintInfoDaoImpl implements ComplaintInfoDao {

	@PersistenceContext(name="un")
	private EntityManager manager;

	@Override
	public List<ComplaintInfo> findAll() {
		// TODO Auto-generated method stub
		String jpql="select comInfo from ComplaintInfo comInfo";
		List<ComplaintInfo>complaintInfoList=manager.createQuery(jpql).getResultList();
		if (complaintInfoList.isEmpty()) {
			return null;
		}
		else return complaintInfoList;
	}

	@Override
	public List<ComplaintInfo> findByMerchantId(String merchantId) {
		// TODO Auto-generated method stub
		String jpql="select comInfo from ComplaintInfo comInfo where comInfo.merchant.id=:merchantId";
		List<ComplaintInfo>complaintInfoList=manager.createQuery(jpql).setParameter("merchantId", merchantId).getResultList();
		if (complaintInfoList.isEmpty()) {
			return null;
		}
		else return complaintInfoList;
	}

	@Override
	public ComplaintInfo findById(String id) {
		// TODO Auto-generated method stub
		ComplaintInfo complaintInfo=manager.find(ComplaintInfo.class, id);
		return complaintInfo;
	}

	@Override
	public ComplaintInfo add(ComplaintInfo complaintInfo) {
		// TODO Auto-generated method stub
		manager.persist(complaintInfo);
		return complaintInfo;
	}

	@Override
	public List<Merchant> findAllByMerchant() {
		// TODO Auto-generated method stub
		String jpql = "select m from Merchant m";
		List<Merchant> merchants = manager.createQuery(jpql)
								.getResultList();
		if(merchants.isEmpty()){
			return null;
		}
		return merchants;
	}

	
	
}
